package com.ysd.lis.service.lab.impl;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.lab.LabItemDictCritical;
import com.ysd.lis.mapper.lab.LabItemDictCriticalMapper;
import com.ysd.lis.service.lab.LabItemDictCriticalService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 报告项目危急值 服务实现类
 * </p>
 *
 * @author wanghao
 * @since 2024-01-27
 */
@Service
public class LabItemDictCriticalServiceImpl extends ServiceImpl<LabItemDictCriticalMapper, LabItemDictCritical> implements LabItemDictCriticalService {
    @Autowired
    private LabItemDictCriticalMapper criticalMapper;

    @Override
    public Result findItemDictCritical(LabItemDictCritical labItemDictCritical) {
        if (ToolsUtils.isEmpty(labItemDictCritical)) {
            return Result.fail("参数不可为空");
        }
        if (ToolsUtils.isEmpty(labItemDictCritical.getItemNo())) {
            return Result.fail("报告项目ID不可为空");
        }

        SysUser sysUser = RedisUserManager.getUser();
        MPJLambdaWrapper<LabItemDictCritical> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(LabItemDictCritical.class)
                .selectAs("LID", LabItemDict::getItemNa, LabItemDictCritical::getItemNa)
                .leftJoin(LabItemDict.class, "LID",p->p.eq(LabItemDict::getItemNo, LabItemDictCritical::getItemNo).eq(LabItemDict::getRptGroup, LabItemDictCritical::getRptGroup).eq(LabItemDict::getDelFlag,0));
        if (ToolsUtils.isNotEmpty(sysUser)) {
            labItemDictCritical.setOrgId(sysUser.getOrgId());
            lambdaQueryWrapper.eq(LabItemDictCritical::getOrgId, labItemDictCritical.getOrgId());
        }
        lambdaQueryWrapper
                .eq(LabItemDictCritical::getItemNo, labItemDictCritical.getItemNo())
                .eq(LabItemDictCritical::getRptGroup, labItemDictCritical.getRptGroup())
                .eq(LabItemDictCritical::getDelFlag, 0);
        List<LabItemDictCritical> list = criticalMapper.selectList(lambdaQueryWrapper);
        return Result.succ(list);
    }

    @Override
    public Result saveOrUpdateCritical(List<LabItemDictCritical> labItemDictCriticals) {
        SysUser sysUser = RedisUserManager.getUser();

        List<LabItemDictCritical> collect = labItemDictCriticals.stream().filter(item -> StringUtils.isNotBlank(item.getDisplayText())).collect(Collectors.toList());

        for (LabItemDictCritical critical : collect) {
            if (StringUtils.isBlank(critical.getId())) {
                critical.setOrgId(sysUser.getOrgId());
                critical.setIsEnable("1");
            }
        }
        boolean b = this.saveOrUpdateBatch(labItemDictCriticals);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteItemCritical(LabItemDictCritical labItemDictCritical) {
        if (ToolsUtils.isEmpty(labItemDictCritical)) {
            return Result.fail("入参不可为空！");
        }
        //是否需要判断有没有相关业务正在使用这个规则，先走流程。此处需要确认
        int delete = criticalMapper.deleteById(labItemDictCritical.getId());
        if (delete > 0) {
            return Result.succ(1, "删除成功！", null);
        } else {
            return Result.fail("删除失败！");
        }
    }
}
